package com.boboPlanet.user.controller;

import com.boboPlanet.comm.context.LoginUserContext;
import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.result.ResponseResult;
import com.boboPlanet.user.entity.dto.MenuDTO;
import com.boboPlanet.user.entity.po.RolePO;
import com.boboPlanet.user.entity.vo.CurrentUserInfoVO;
import com.boboPlanet.user.entity.vo.MenuVO;
import com.boboPlanet.user.entity.vo.RoleMenuDTO;
import com.boboPlanet.user.entity.vo.UserRoleVO;
import com.boboPlanet.user.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/2/21
 **/
@RestController
@RequestMapping("/planetMenus")
@Api(tags = "波仔星球菜单信息", value = "波仔星球菜单信息")
@Slf4j
public class SysManageController {

    @Resource
    private MenuService menuService;

    @GetMapping("/getAllMenus")
    @ApiOperation("获取所有菜单信息")
    public ResponseResult<List<MenuVO>> getAllMenus(){
        return ResponseResult.isSuccess(menuService.getAllMenus());
    }

    @GetMapping("/getNotLoginMenus")
    @ApiOperation("获取未登录菜单信息")
    public ResponseResult<List<MenuVO>> getNotLoginMenus(){
        if (LoginUserContext.getLoginUserInfoPO() != null){
            return ResponseResult.error();
        }
        return ResponseResult.isSuccess(menuService.getNotLoginMenus());
    }

    @GetMapping("/getLoginMenus")
    @ApiOperation("获取登录菜单信息")
    public ResponseResult<List<MenuVO>> getLoginMenus(){
        if (LoginUserContext.getLoginUserInfoPO() == null){
            return ResponseResult.error();
        }
        return ResponseResult.isSuccess(menuService.getLoginMenus());
    }

    @PostMapping("/addMenu")
    @ApiOperation("添加星球菜单")
    public ResponseResult<Boolean> addMenu(@RequestBody MenuDTO dto){
        return ResponseResult.isSuccess(menuService.addMenu(dto));
    }

    @GetMapping("/findParentMenus")
    @ApiOperation("获取父级菜单菜单信息")
    public ResponseResult<List<MenuVO>> findParentMenus(){
        return ResponseResult.isSuccess(menuService.findParentMenus());
    }

    /**
     * 获取角色列表
     * @param role RoleName
     * @param isAll true:获取所有(角色管理)  false:获取正常(分配菜单时)
     * @return List
     */
    @GetMapping("/getRoleList")
    @ApiOperation("获取角色列表")
    public ResponseResult<List<RolePO>> getRoleList(String role,boolean isAll){
        return ResponseResult.isSuccess(menuService.getRoleList(role,isAll));
    }

    @GetMapping("/addRole")
    @ApiOperation("添加角色")
    public ResponseResult<Boolean> addRole(@RequestParam String role){
        return ResponseResult.isSuccess(menuService.addRole(role));
    }

    @GetMapping("/delRole/{roleId}")
    @ApiOperation("作废角色")
    public ResponseResult<Boolean> delRole(@PathVariable Long roleId){
        return ResponseResult.isSuccess(menuService.delRole(roleId));
    }

    @GetMapping("/showRole/{roleId}")
    @ApiOperation("恢复角色")
    public ResponseResult<Boolean> showRole(@PathVariable Long roleId){
        return ResponseResult.isSuccess(menuService.showRole(roleId));
    }

    @GetMapping("/getSysUserInfo")
    @ApiOperation("获取系统用户信息")
    public ResponseResult<List<CurrentUserInfoVO>> getSysUserInfo(String content){
        return ResponseResult.isSuccess(menuService.getSysUserInfo(content));
    }

    /**
     * @param userId 根据userId获取角色
     * @return RolePO
     */
    @GetMapping("/getRolesByUserId/{userId}")
    @ApiOperation("根据userId获取角色列表")
    public ResponseResult<List<UserRoleVO>> getRolesByUserId(@PathVariable Long userId){
        return ResponseResult.isSuccess(menuService.getRolesByUserId(userId));
    }

    /**
     * @param userId Long
     * @param roleIds String
     * @return Boolean
     */
    @GetMapping("/setRoleForUser/{userId}/{roleIds}")
    @ApiOperation("给用户分配角色")
    public ResponseResult<Boolean> setRoleForUser(@PathVariable Long userId,@PathVariable String roleIds){
        if (roleIds == null || roleIds.length() <= 0){
            throw BusinessException.build("请选择有效的角色信息,不可为空!!!");
        }
        String[] roleIdArr = roleIds.split(",");
        return ResponseResult.isSuccess(menuService.setRoleForUser(userId,roleIdArr));
    }

    @GetMapping("/getMenuInfoByRoleId/{roleId}")
    @ApiOperation("获取角色菜单信息")
    public ResponseResult<RoleMenuDTO> getMenuInfoByRoleId(@PathVariable Long roleId){
        return ResponseResult.isSuccess(menuService.getMenuInfoByRoleId(roleId));
    }

    @GetMapping("/distributeMenuForRole")
    @ApiOperation("给角色分配菜单")
    public ResponseResult<Boolean> distributeMenuForRole(@RequestParam Long roleId,@RequestParam List<Long> menuIds){
        return ResponseResult.isSuccess(menuService.distributeMenuForRole(roleId,menuIds));
    }

}
